package com.baidu.controller;


import com.baidu.pojo.Exhibition;
import com.baidu.pojo.ReturnJson;
import com.baidu.pojo.Ticket;
import com.baidu.pojo.User;
import com.baidu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public String login(User user, HttpSession session) {
            User user1=userService.login(user);
            if (user1!=null){
                System.out.println("SUCCESS");
                //登录成功
                session.setAttribute("user1",user1);
                return "person";
            }else {
                System.out.println("FAIL");
                //登录失败
                return "redirect:/login.jsp";
            }
    }
    @RequestMapping("/regUser")
    @ResponseBody
    public ReturnJson regUser(User user){
        ReturnJson returnJson = new ReturnJson();
        Date  date =new Date();
        user.setUtime(date);
        System.out.println("user"+user);
        userService.regUser(user);
        returnJson.setCode(200);
        returnJson.setMsg("成功");
        System.out.println("regUser");
        return returnJson;
    }
    @RequestMapping("/goCenter")
    public String goCenter(){
        System.out.println("goCenter");
        return "person";
    }
    //    个人
    @RequestMapping("/goPersonCenter")
    public String goPersonCenter(){
        System.out.println("goPersonCenter");
        return "personCenter";
    }
    //    评价
    @RequestMapping("/goAssessCenter")
    public String goAssessCenter(){
        System.out.println("goAssessCenter");
        return "AssessCenter";
    }
    //    充值
    @RequestMapping("/goMoneyCenter")
    public String goMoneyCenter(){
        System.out.println("goMoneyCenter");
        return "moneyCenter";
    }
    @RequestMapping("addUserCount")
    public String addUserCount(HttpSession session, Integer num) {
        User u = (User)session.getAttribute("user1");
        System.out.println("u:"+u);
        Integer mm = u.getUcount();
        u.setUcount(mm+num);
        userService.addUserCount(u);
//      更新session
        session.setAttribute("user1",userService.getUserById(u.getUid()));
        return "redirect:/user/goMoneyCenter";
    }
    @RequestMapping("updateUser")
    public String addUserCount(HttpSession session,User u) {
        userService.updateUser(u);
//      更新session
        session.setAttribute("user1",userService.getUserById(u.getUid()));
        return "redirect:/user/goPersonCenter";
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        System.out.println("---logout---");
//        销毁Session
        session.invalidate();

        return "redirect:/login.jsp";
    }





}
